<template>
  <div>
    <el-upload
      ref="upload"
      class="avatar-uploader"
      action
      :multiple="multiple"
      :on-preview="handlePreview"
      :before-remove="beforeRemove"
      :on-remove="handleRemove"
      :file-list="fileList"
      :on-change="handleChange"
      :before-upload="beforeAVatarUpload"
      :on-exceed="onExceed"
      :on-error="uploadError"
      :on-success="uploadSuccess"
      :http-request="upload"
      :show-file-list="canUpload"
      :disabled="disabled"
    >
      <div v-if="uploadStyle === 'btn'">
        <el-button size="small" type="primary">点击上传</el-button>
      </div>
      <div v-else>
        <img v-if="imageUrl" :src="imageUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon" />
      </div>
    </el-upload>
  </div>
</template>

<script>
import { browserType } from '@/utils/validate'
import { doUploadFile } from '@/api/upload/upload'
export default {
  name: 'KUploadSdkFile',
  props: {
    value: { // 引用此组件传过来的路径。因为是一个。所以String类型
      type: String,
      default: ''
    },
    uploadUrl: { // 传递后台的路径
      type: String,
      default: ''
    },
    limit: { // 限制几个.默认是1
      type: Number,
      default: 1
    },
    uploadStyle: { // 限制的上传形式。img还是btn，默认是btn形式
      type: String,
      default: 'btn'
    },
    multiple: {
      type: Boolean,
      default: false
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      fileList: [],
      imageUrl: '',
      canUpload: true
    }
  },
  watch: {
    // filelist 回显使用
    value: {
      deep: true,
      immediate: true,
      handler() {
        if (this.uploadStyle === 'btn') {
          if (!this.value) return
          this.fileList = []
          const file = {}
          file.name = this.value.substring(this.value.lastIndexOf('/') + 1)
          file.url = this.value
          this.fileList.push(file)
        } else {
          this.imageUrl = this.value
        }
      }
    }
  },
  created() {
  },
  methods: {
    handleChange(file, fileList) {
      if (fileList.length > 0) {
        this.fileList = []
        this.fileList.push(file)
      }
    },
    beforeRemove(file) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    handleRemove(file, fileList) {
      this.resetFile()
    },
    handlePreview(file) {
      window.open(file.url, '_blank')
    },
    onExceed(files, fileList) {
      this.$message.error('提示：只能上传' + this.limit + '个文件！')
    },
    // 图片上传
    beforeAVatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 100
      if (!isLt2M) {
        this.canUpload = false
        this.fileList = []
        this.$message.error('上传文件大小不能超过 100MB!')
      } else {
        this.canUpload = true
      }
    },
    uploadSuccess(response, file, fileList) {
      this.fileIds = response.fileIds
    },
    uploadError(response, file, fileList) {
      this.$message.error('上传失败！')
    },
    upload(file) {
      if (!this.canUpload) return
      const formData = new FormData()
      if (browserType('ie')) {
        formData.append('upLoadFile', file.file)
      } else {
        formData.set('upLoadFile', file.file)
      }
      doUploadFile(formData).then(res => {
        this.$message.success('上传成功！')
        this.$emit('successUpload', res.data)
      })
    },
    resetFile() {
      this.imageUrl = ''
      this.fileList = []
    }
  }
}
</script>

<style lang="scss" scoped>
$mainColor:#3b425d;// 主色调

 ::v-deep .avatar-uploader{
    .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }
    .el-upload:hover {
      border-color: $mainColor;
    }
    &-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
    }
   .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
 }

</style>
